package com.meals.system.api.seller;

import com.meals.system.api.admin.param.BatchIdParam;
import com.meals.system.api.user.vo.WhitesMealOrderDetailVO;
import com.meals.system.api.user.vo.WhitesMealOrderListVO;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.config.annotation.TokenToSeller;
import com.meals.system.entity.SystemSeller;
import com.meals.system.entity.SystemSellerToken;
import com.meals.system.service.WhitesMealOrderService;
import com.meals.system.util.PageQueryUtil;
import com.meals.system.util.PageResult;
import com.meals.system.util.Result;
import com.meals.system.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@RestController
@Api(value = "v1", tags = "2-2.白餐商家订单操作相关接口")
@RequestMapping("/seller-api/v1")
public class WhitesMealSellerOrderAPI {

    private static final Logger logger = LoggerFactory.getLogger(WhitesMealSellerOrderAPI.class);

    @Resource
    private WhitesMealOrderService whitesMealOrderService;

    /**
     * 列表
     */
    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    @ApiOperation(value = "订单列表", notes = "可根据订单号和订单状态筛选")
    public Result list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                       @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize,
                       @RequestParam(required = false) @ApiParam(value = "订单号") String orderNo,
                       @RequestParam(required = false) @ApiParam(value = "订单状态") Integer orderStatus, @TokenToSeller SystemSellerToken loginSeller){
        logger.info("seller:{}", loginSeller.toString());
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10) {
            return ResultGenerator.genFailResult("分页参数异常！");
        }
        Map params = new HashMap(8);
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        if (!StringUtils.isEmpty(orderNo)) {
            params.put("orderNo", orderNo);
        }
        if (orderStatus != null) {
            params.put("orderStatus", orderStatus);
        }
        // 将sellerId 传到param参数中，包装给pageUtil
        params.put("sellerId",loginSeller.getSellerId());
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(whitesMealOrderService.getWhitesMealOrdersPage(pageUtil));
    }

    /**
     * 详情
     */
    @GetMapping("/orders/{orderId}")
    @ApiOperation(value = "订单详情", notes = "传参为订单号")
    public Result<WhitesMealOrderDetailVO> orderDetailPage(@ApiParam(value = "订单号") @PathVariable("orderId") Long orderId, @TokenToSeller SystemSellerToken loginSeller){
        logger.info("seller:{}", loginSeller.toString());
        return ResultGenerator.genSuccessResult(whitesMealOrderService.getOrderDetailByOrderId(orderId));
    }

    /**
     * 配货
     */
    @RequestMapping(value = "/orders/checkDone", method = RequestMethod.PUT)
    @ApiOperation(value = "修改订单状态为配货成功", notes = "批量修改")
    public Result checkDone(@RequestBody BatchIdParam batchIdParam, @TokenToSeller SystemSellerToken loginSeller){
        logger.info("seller:{}", loginSeller.toString());
        if (batchIdParam == null || batchIdParam.getIds().length < 1){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result = whitesMealOrderService.checkDone(batchIdParam.getIds());
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    /**
     * 出库
     */
    @RequestMapping(value = "/orders/checkOut", method = RequestMethod.PUT)
    @ApiOperation(value = "修改订单状态为已出库", notes = "批量修改")
    public Result checkOut(@RequestBody BatchIdParam batchIdParam, @TokenToSeller SystemSellerToken loginSeller){
        logger.info("seller:{}", loginSeller.toString());
        if (batchIdParam == null || batchIdParam.getIds().length < 1){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result = whitesMealOrderService.checkOut(batchIdParam.getIds());
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


    /**
     * 关闭订单
     */
    @RequestMapping(value = "/orders/close", method = RequestMethod.PUT)
    @ApiOperation(value = "修改订单状态为商家关闭", notes = "批量修改")
    public Result closeOrder(@RequestBody BatchIdParam batchIdParam, @TokenToSeller SystemSellerToken loginSeller){
        logger.info("seller:{}", loginSeller.toString());
        if (batchIdParam == null || batchIdParam.getIds().length < 1){
            return ResultGenerator.genFailResult("参数异常");
        }
        String result = whitesMealOrderService.closeOrder(batchIdParam.getIds());
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }


}
